package com.shuqing.controller;


import com.shuqing.domain.Book;
import com.shuqing.domain.Result;
import com.shuqing.domain.User;
import com.shuqing.service.BookService;
import com.shuqing.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/selectNewbooks")
    public ModelAndView selectNewBooks() {
        int pageNum = 1;
        int pageSize = 5;
        PageResult pageResult =
                bookService.selectNewBooks(pageNum, pageSize);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("books_new");
        modelAndView.addObject("pageResult", pageResult);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/findById")
    public Result<Book> findById(Integer id){
        Book book = bookService.findById(id);
        return new Result<>(true,"查询成功",book);

    }

    @ResponseBody
    @RequestMapping("/borrowBook")
    public Result borrowBook(Book book, HttpSession session)
    {
        //获取当前登录用户名
        String pname =
                ((User)session.getAttribute("USER_SESSION")).getName();
        book.setBorrower(pname);
        Integer count = bookService.borrowBook(book);
        if (count != 1){
            return new Result(false,"借阅失败！");
        }
        return new Result(true,"借阅成功，请到行政中心取书！");
    }

    @ResponseBody
    @RequestMapping("/search")
    public ModelAndView search(Book book, Integer pageNum,
                               Integer pageSize, HttpServletRequest request) {
        if (pageNum == null){
            pageNum = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
        //查询到的图书信息
        PageResult pageResult = bookService.search(book,pageNum, pageSize);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("books");
        modelAndView.addObject("pageResult", pageResult);
//        回显查询输入框的数据
        modelAndView.addObject("search", book);
        modelAndView.addObject("pageNum", pageNum);
        //将当前查询的控制器路径返回到页面，页码变化时继续项该路径发送请求
        modelAndView.addObject("gourl",request.getRequestURI());
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/addBook")
    public Result addBook(Book book){
        Integer count = bookService.addBook(book);
        if(count != 1){
            return new Result(false , "添加图书失败");
        }else {
            return new Result(true , "添加图书成功");
        }
    }

    @ResponseBody
    @RequestMapping("/editBook")
    public Result editBook(Book book){
        Integer count = bookService.editBook(book);
        if(count != 1){
            return new Result(false, "修改图书失败!");
        }else {
            return new Result(true , "修改成功!");
        }
    }

    @RequestMapping("/searchBorrowed")
    public ModelAndView searchBorrowed(Book book, HttpSession session, Integer pageNum, Integer pageSize,HttpServletRequest request){
        if(pageNum == null){
            pageNum = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
//        获取当前等登录的用户
        User user = (User)session.getAttribute("USER_SESSION");
        PageResult pageResult = bookService.searchBorrowed(book, user, pageNum, pageSize);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("book_borrowed");
//        将查询结果返回封面页
        modelAndView.addObject("pageResult", pageResult);
//        将调查结果回显到查询的输入框
        modelAndView.addObject("search", book);
//        显示页码
        modelAndView.addObject("pageNum", pageNum);
        //将当前查询的控制器路径返回到页面，页码变化时继续项该路径发送请求 （当前页面，改变页码时，路径不变）
        modelAndView.addObject("gourl",request.getRequestURI());
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/returnBook")
    public Result returnBook(Integer id,HttpSession session){
//        获取当前登录用户名
        User user = (User)session.getAttribute("USER_SESSION");
        boolean flag = bookService.returnBook(id, user);
        if (!flag){
            return new Result(true,     "图书还书成功!");
        }else {
            return new Result(false , "图书还书失败!");
        }
    }

    @ResponseBody
    @RequestMapping("/returnConfirm")
    public Result returnConfirm(Integer id){
        Integer flag = bookService.returnConfirm(id);
        if (flag != 1){
            return new Result(false,"确认还书失败");
        }
        return new Result(true,"确认还书成功");

    }



}
